package com.kycxika.app.activity;

import android.content.DialogInterface;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import com.kycxika.app.R;
import com.kycxika.app.activity.fragment.BlogFragment;
import com.kycxika.app.activity.fragment.HomeFragment;
import com.kycxika.app.activity.fragment.MySelfFragment;
import com.kycxika.app.activity.fragment.VideoFragment;

import static com.kycxika.app.KycXikaApplication.context;


public class MainActivity extends BaseFragmentActivity{

    private FragmentTabHost mTabHost;
    private View indicator = null;
    private ImageView mTab_image;
    private TextView mTab_title;
    private LayoutInflater layoutInflater;
    private Class fragmentArray[] = {HomeFragment.class, BlogFragment.class,VideoFragment.class,MySelfFragment.class};
    public static MainActivity instance;
    //定义数组来存放按钮图片
    private int mImageViewArray[] = {R.drawable.home_selector,R.drawable.boke_selector,R.drawable.video_selector,R.drawable.family_selector};

    //Tab选项卡的文字
    private String mTextviewArray[] = {"主页","博客","视频","个人"
           /* this.getResources().getString(R.string.home_title),
            this.getResources().getString(R.string.school_title),
            this.getResources().getString(R.string.family_title)*/};
    private View mTop;

    @Override
    protected void initView() {
        instance = this;
        mTop = findViewById(R.id.top);
        setTop(mTop);
        layoutInflater = LayoutInflater.from(this);
        /*就是把指定的布局资源注射到指定的容器*/
        getTitle(mTextviewArray[0]);
        getVisibility(false,MainActivity.this);
        //实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        //R.id.realtabcontent 这里是一个FrameLayout标签
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        //得到fragment的个数
        int count = fragmentArray.length;

        for(int i = 0; i < count; i++){
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray[i], null);

            //设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(/*R.color.c_F0F0F0*/R.drawable.bg_daohang);
        }
    }

    private View getTabItemView(int index){
        View inflate = View.inflate(this, R.layout.tabhost_layout, null);
        mTab_image = (ImageView) inflate.findViewById(R.id.tab_image);
        mTab_title = (TextView) inflate.findViewById(R.id.tab_title);
        mTab_image.setImageResource(mImageViewArray[index]);
        mTab_title.setText(mTextviewArray[index]);
        return inflate;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                getTitle(tabId);
                getVisibility(false,MainActivity.this);
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    private int progressDown;
    private String downSuccess;

    public int getProgressDown() {
        return progressDown;
    }

    public void setProgressDown(int progressDown) {
        this.progressDown = progressDown;
    }

    public String getDownSuccess() {
        return downSuccess;
    }

    public void setDownSuccess(String downSuccess) {
        this.downSuccess = downSuccess;
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(context)
                .setCancelable(true)
                .setTitle("系统退出")
                .setMessage("确定退出当前程序吗?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        MainActivity.this.finish();

                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).show();
    }
}
